package Controller;

import Utils.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import Service.StockService;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Map;
@Controller
public class StockController
{
    @Autowired
    private StockService stockService;
    //获取全部股票数据
    @ResponseBody
    @RequestMapping(value = "/API/Stock",method = RequestMethod.GET)
    public JsonResult GetAllStock()
    {
        //创建Json返回对象
        JsonResult jsonResult = null;
        jsonResult = new JsonResult();
        try{
            ArrayList<StockInfo> stockinfos = stockService.getAllStockInfo();
            jsonResult.setData(stockinfos);
            jsonResult.setErrorCode("调用成功");
        }
        catch (Exception e)
        {
            jsonResult.setErrorCode("发生异常，请联系管理员处理");
        }
        finally {
            return jsonResult;
        }
    }
    //获取单个股票数据
    @ResponseBody
    @RequestMapping(value = "/API/Stock/{Code}",method = RequestMethod.GET)
    public JsonResult GetStockByCode(@PathVariable String Code)
    {
        while(Code.length() < 6)
        {
            Code = "0"+Code;
        }
        //创建Json返回对象
        JsonResult jsonResult = null;
        jsonResult = new JsonResult();
        try{
            StockInfo stockInfo = stockService.getStockInfoByCode(Code);
            if(stockInfo != null)
            {
                jsonResult.setData(stockInfo);
                jsonResult.setErrorCode("调用成功");
            }
            else{
                jsonResult.setErrorCode("调用成功，该股不存在或该股已退市");
            }
        }
        catch (Exception e)
        {
            jsonResult.setErrorCode("发生异常，请联系管理员处理");
        }
        finally {
            return jsonResult;
        }
    }
    //查询所有股票数据
    @ResponseBody
    @RequestMapping(value = "/API/Stock/GetAllStock",method = RequestMethod.GET)
    public JsonResult GetStock()
    {
        //创建Json返回对象
        JsonResult jsonResult = null;
        jsonResult = new JsonResult();
        try{
            ArrayList<Stock> stockArrayList = null;
            stockArrayList = stockService.getAllStock();
            jsonResult.setData(stockArrayList);
            jsonResult.setErrorCode("调用成功");
        }
        catch (Exception e)
        {
            jsonResult.setErrorCode("发生异常，请联系管理员处理");
        }
        finally {
            return jsonResult;
        }
    }
    @ResponseBody
    @RequestMapping(value = "/API/Stock/GetStockHistory",method = RequestMethod.POST)
    public JsonResult GetStockHistory(@RequestBody String params)
    {
        //创建Json返回对象
        JsonResult jsonResult = null;
        jsonResult = new JsonResult();
        try{
            //解析前端请求Json
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String,String> params_map = objectMapper.readValue(params,Map.class);
            String StockCode = params_map.get("StockCode");
            while(StockCode.length() < 6)
            {
                StockCode = "0"+StockCode;
            }
            ArrayList<StockHistoryInfo> stockHistoryInfosList = GetStockInfo.GetHistoryStockInfo(StockCode);
            jsonResult.setData(stockHistoryInfosList);
            jsonResult.setErrorCode("调用成功");
        }
        catch (Exception e)
        {
            jsonResult.setErrorCode("发生异常，请联系管理员处理"+e.toString());
        }
        finally {
            return jsonResult;
        }
    }
}
